package com.hf.cllectiontools;

import java.util.*;

public class TestDriver {
    public static void main(String[] args) {
        test01();
        System.out.println("======================================");
        test02();
        System.out.println("=========================================");
        test03();
    }

    /**
     * 实现Comparator接口
     */
    public static void test03(){
        //对list集合的各个元素排序
        List<Teacher> list = new ArrayList();
        list.add(new Teacher("He Jin","2032",98));
        list.add(new Teacher("Bai Qi nan","2042",98));
        list.add(new Teacher("Gan Hi","2032",98));
        list.add(new Teacher("Jhu","2032",95));
        list.add(new Teacher("Een la","2032",99));
        list.add(new Teacher("Lids","2032",97));
        list.add(new Teacher("Zhang","2032",78));
        list.add(new Teacher("Wu Qi fan","2032",76));
        list.add(new Teacher("Ai","2032",65));
        list.add(new Teacher("Ni","2032",97));
        Set set = new TreeSet();
        //发生运行时异常吗？使Student类实现 Comparable 接口
        System.out.println("排序前：");
        prTea(list);
//        匿名内部类
        Collections.sort(list, new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                //默认 等于
                int i = 0;
                if (o1.getTeaSore() > o2.getTeaSore()){
                    i = 1;
                }
                if (o1.getTeaSore() < o2.getTeaSore()){
                    i = -1;
                }
                return i;
            }
        });
        System.out.println("排序后：");
        prTea(list);
    }

    /**
     * 实现Comparable接口
     */
    public static void test02(){
        //对list集合的各个元素排序
        List<Student> list = new ArrayList();
        list.add(new Student("He Jin","2032",98));
        list.add(new Student("Bai Qi nan","2042",98));
        list.add(new Student("Gan Hi","2032",98));
        list.add(new Student("Jhu","2032",95));
        list.add(new Student("Een la","2032",99));
        list.add(new Student("Lids","2032",97));
        list.add(new Student("Zhang","2032",78));
        list.add(new Student("Wu Qi fan","2032",76));
        list.add(new Student("Ai","2032",65));
        list.add(new Student("Ni","2032",97));
        //Set set = new TreeSet();
        //发生运行时异常吗？使Student类实现 Comparable 接口
        System.out.println("排序前：");
        prStu(list);
        Collections.sort(list);
        System.out.println("排序后：");
        prStu(list);
    }

    public static void test01(){
        //对list集合的各个元素排序
        List<String> list = new ArrayList();
        list.add("He Jin");
        list.add("Bai Qi nan");
        list.add("Gan Hi");
        list.add("Jhu");
        list.add("Een la");
        list.add("Lids");
        list.add("Zhang");
        list.add("Wu Qi fan");
        list.add("Ai");
        list.add("Ni");
        System.out.println("排序前：");
        prList(list);
        //排序
        Collections.sort(list);
        System.out.println("排序后：");
        prList(list);
    }

    /**
     * 打印list中的Teacher元素对象
     * @param list list集合
     */
    public static void prTea(List<Teacher> list){
        for (Teacher tea : list){
            System.out.print(tea.getTeaName()+"    ");
            System.out.println("    "+tea);

        }
        System.out.println();
    }

    /**
     * 打印list中的Student元素对象
     * @param list list集合
     */
    public static void prStu(List<Student> list){
        for (Student stu : list){
            System.out.print(stu.getStuName()+"    ");
            System.out.println("    "+stu);
        }
        System.out.println();
    }



    /**
     * 打印list中的String元素对象
     * @param list list集合
     */
    public static void prList(List<String> list){
        for (String str : list){
            System.out.print("    "+str);
        }
        System.out.println();
    }

}
